package com.zhibang.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhibang.dto.AccountxtzhDTO;
import com.zhibang.entity.Account;
import com.zhibang.service.AccountService;
import com.zhibang.util.Result;
import com.zhibang.vo.AccountAddVO;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RequiresUser
@RestController
@RequestMapping("/accounts")
public class AccountsController {
    @Resource
    private AccountService accountService;

//    @RequestMapping("findAccount/{pageNum}/{pageSize}")
//    public Result list(
//            @PathVariable Integer pageNum,
//            @PathVariable Integer pageSize,
//            @RequestBody AccountxtzhDTO query) {
//        IPage<AccountxtzhDTO> pager = new Page<>(pageNum, pageSize);
//        accountService.selectAccountInfo(query, pager);
//        return Result.data(pager);
//    }

    @RequestMapping("checkAccount/{account}")
    public Result check(@PathVariable String account) {
        Account account1 = accountService.getOne(new QueryWrapper<Account>().eq("account", account));
        if (account1 != null) {
            return Result.data("true");
        } else {
            return Result.data("false");

        }
    }

    @RequestMapping("saveAccount")
    public Result save(@RequestBody AccountAddVO account) {
        int add = accountService.addAccount(account);
//       System.out.println(add);
        if (add > 0) {
            return Result.data("true");
        } else {
            return Result.data("false");
        }
    }

    @RequestMapping("findAccountById/{id}")
    public Result findById(@PathVariable Integer id) {
        Account account = accountService.getAccountById(id);
        if (account != null) {
            return Result.data(account);
        } else {
            return Result.data("false");
        }
    }

    @RequestMapping("updateAccount")
    public Result update(@RequestBody AccountAddVO account) {
        int update = accountService.updateAccount(account);
        if (update > 0) {
            return Result.data("true");
        } else {
            return Result.data("false");
        }
    }
}
